﻿(function ($) {
    if ($.uploadFile) return false; //防止JS重复加载
    $.fn.uploadFile = function (options) {
        this.each(function () {
            if (!$.nodeName(this, 'INPUT')) { return; }
            if (!this.uploadFile) {
                var upload = new $.uploadFile(this, options);
                if (upload.init()) {
                    this.uploadFile = upload;
                }
                else upload = null;
            }
        });
    }

    $.uploadFile = function (input, options) {
        var _this = this;
        var _nextObj = null;

        if (options.isWatermark == null)
            options.isWatermark = '0';

        this.init = function () {
            $(input).hide();
            _this.showForm();
            _this.showOneValue(options.val);
            return true;
        }

        this.showForm = function () {
            _nextObj = $(input).next('div');
            if (_nextObj.size() == 0 || _nextObj.eq(0).attr('class') != 'uploadfileContainer') {
                $(input).after('<div class="uploadfileContainer"></div>');
                _nextObj = $(input).next('.uploadfileContainer').eq(0);
            }
            else {
                _nextObj = $(input).next('.uploadfileContainer').eq(0);
                return;
            }
            _nextObj.append('<div class="uploadfileOne"><div class="uploadfileInfo"></div><iframe name="uploadfileForm" src="about:blank" frameborder="0" scrolling="no" style="height:40px; width:300px; " allowTransparency="true"></iframe></div>');
            _nextObj.find('iframe').eq(0).bind('load', function () {
                var _value = $(this).contents().find('body').html();
                if (_value != "false" && _value != null && _value != "") {
                    _this.success(_value);
                }
            });

            setTimeout(function () {
                _nextObj.find('iframe').each(function () {
                    _this.showOneForm();
                });
            }, 250);
        }

        // 组装 form 和上传组件
        this.showOneForm = function () {
            var type = options.type;
            var url = "/handler/postFile.ashx";
            //var t = $("#uploadtype").val();
            var input = "";
            input = "<input type=\"button\"  name=\"upload\" value='Browse' id=\"upload\" />"

            var _objFrame = _nextObj.find('iframe').eq(0);
            //   _objFrame.contents().find('head').append('<link href="/themes/default/css/StyleSheet.css" type="text/css" rel="stylesheet"/>');
            _objFrame.contents().find('body').html('<div class="add"><div id="uploadCtr" ><form class="picForm" action="' + url + '" method="post" enctype="multipart/form-data"><input type="hidden" name="eType" value=' + type + ' /><input type="hidden" name="isReferrer" id="isReferrer" value="1"><input type="file" name="upfile" id="fileUpload" style="width: 80px; font-size: 20px; position: absolute;"  /></form>' + input + '</div></div>');
            _objFrame.contents().find('#fileUpload').css("opacity", 0);
            _objFrame.contents().find('#fileUpload').bind('change', function () {
                _this.postFile();
                $(this).get(0).form.submit();
            });
        }

        // 上传文件中样式
        this.postFile = function () {
            //$(".uploadfileInfo").attr("style", "height:16px; width:100px; float:left;line-height:16px;");
            _nextObj.find('.uploadfileInfo').eq(0).html('<img src="http://imagecn.gasgoo.com/sns/default/images/purchas/loading.gif" align="middle" />');
            _nextObj.find('.uploadfileInfo').eq(0).show();
            _nextObj.find('iframe').eq(0).hide();
        }

        // 存在图片
        this.showOneValue = function (value) {
            if (value != '') {
                var t = $("#type").val();
                if (t == "Merchant" || t == "Stock") {
                    _nextObj.find('.uploadfileInfo').eq(0).html('<span style="float:left; height:13px; ">Remove</span><a name="reupload" class="iconClose"></a>');
                } else {
                    _nextObj.find('.uploadfileInfo').eq(0).html('<span style="float:left; height:13px; margin-top:9px;">Remove</span><a name="reupload" style=" display: block;  float: left; height: 13px; margin:12px 0 0 5px; overflow: hidden; width: 12px;"><img src="http://imagecn.gasgoo.com/sns/default/images/fast/close.gif" border="0" alt="删除已上传的文件"/></a>');
                }
                _nextObj.find('iframe').eq(0).hide();
                jQuery(options.key).find("img").eq(0).attr("src", value).show();
                _nextObj.find('.uploadfileInfo').eq(0).find('[name=reupload]').bind('click', function () {
                    _this.ReUpload();
                });
            }
        }

        // 重新上传
        this.ReUpload = function () {
            jQuery(options.key).find("img").eq(0).hide();
            _nextObj.find('.uploadfileInfo').eq(0).html('').hide();
            _this.showOneForm();
            _nextObj.find('iframe').eq(0).show();
            $(input).val($(input).val());
        }

        // 请求成功
        this.success = function (msg) {
            try {
                uploadSuccess(eval("(" + msg + ")"), options);
                //_nextObj.find('.uploadfileInfo').eq(0).html("");

                var t = $("#type").val();
                if (t == "Merchant" || t == "Stock") {
                    _nextObj.find('.uploadfileInfo').eq(0).html('<span style="float:left; height:13px; ">Remove</span><a name="reupload" class="iconClose"></a>');
                } else {
                    _nextObj.find('.uploadfileInfo').eq(0).html('<span style="float:left; height:13px; margin-top:9px;">Remove</span><a name="reupload" style=" display: block;  float: left; height: 13px; margin:12px 0 0 5px; overflow: hidden; width: 12px;"><img src="http://imagecn.gasgoo.com/sns/default/images/fast/close.gif" border="0" alt="删除已上传的文件"/></a>');
                }
                _nextObj.find('iframe').eq(0).hide();
                _nextObj.find('.uploadfileInfo').eq(0).find('[name=reupload]').bind('click', function () {
                    _this.ReUpload();
                });


            } catch (e) {
                _this.ReUpload();
            }

        }
    }
})(jQuery);